package yt.kpool.activity.setting;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;
import yt.kpool.bean.BaseBean;
import yt.kpool.bean.ChildAccountAPIKeyBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.LoadingDialog;

/**
 * 生成只读地址
 */
public class CreateOnlyReadActivity extends AppCompatActivity {


    @BindView(R.id.edit_remark)
    EditText edit_remark;
    @BindView(R.id.tv_name)
    TextView tv_name;
    private LoadingDialog loadingDialog;
    public static void start(Activity mactivity, String ChildAccountID,String accountName)
    {
        Intent intent = new Intent(mactivity, CreateOnlyReadActivity.class);
        intent.putExtra("ChildAccountID",ChildAccountID);

        intent.putExtra("accountName",accountName);
        mactivity.startActivityForResult(intent,25);
    }
    public String getChildAccountID() {
        return getIntent().getStringExtra("ChildAccountID");
    }
    public String getAccountName() {
        return getIntent().getStringExtra("accountName");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_only_read);
        ButterKnife.bind(this);
        initTitle();
        tv_name.setText("挖矿账户 "+getAccountName()+" 的只读页面链接");
    }
    private void initTitle() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle("添加只读页面链接");

        loadingDialog = new LoadingDialog(this, 0);
    }

    @OnClick({R.id.btn_create})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_create:
                if ("".equals(edit_remark.getText().toString().trim()))
                {
                    ToastUtil.showToast(CreateOnlyReadActivity.this,"请填写备注");
                }else
                {
                    upData();
                }

                break;


        }
    }

    /**
     * 生成apikey
     */
    private void upData() {
        if (NetWorkUtil.check(this)) {


            loadingDialog.show();
            NetHelperNew.AddChildAccountAPIKey(getChildAccountID(),edit_remark.getText().toString().trim(), new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    loadingDialog.dismiss();
                    Log.i("-->", "生成APIKey：" + data);
                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                    if (baseBean.getType() == 1) {
//                        ChildAccountAPIKeyBean.DataBean data1 = childAccountBean.getData();
//                        OnlyReadActivity.start(CreateOnlyReadActivity.this, getChildAccountID(),data1.getRemark(),data1.getCreateTime(),data1.getAPIKey(),data1.getUrl(),getAccountName());
                        ToastUtil.showToast(getApplicationContext(), baseBean.getContent());
                        setResult(26,getIntent());
                        finish();
                    }else
                    {
                        ToastUtil.showToast(getApplicationContext(), baseBean.getContent());
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);

                    loadingDialog.dismiss();
                    ToastUtil.showToast(getApplicationContext(), msg);
                }
            });


        }

    }
}
